package main

import (
	"fmt"
	"sync"
)

var a []int
var b chan int //通道需要初始化才行

var wg sync.WaitGroup

func main() {

	a := make(chan int, 50)
	b := make(chan int, 100)
	wg.Add(2)
	go f1(a)
	go f2(a, b)
	for ret := range b {
		fmt.Println(ret)
	}
}
func bufCh() {
	b = make(chan int, 10)
	wg.Add(1)
	go func() {
		defer wg.Done()
		x := <-b
		fmt.Println("后台goroutine娶到了值", x)
	}()
	b <- 10

	fmt.Println(b)
	wg.Wait()
}

func f1(ch1 chan int) {
	defer wg.Done()
	for i := 0; i < 100; i++ {
		ch1 <- i
	}
	close(ch1)
}

func f2(ch1 chan int, ch2 chan int) {
	defer wg.Done()
	for {
		x, ok := <-ch1
		if !ok {
			break
		}
		ch2 <- x * x
	}
	close(ch2)
}
